var app = angular.module("addgroupApp",[]);

app.controller("addgroupController",function($scope,$http){
	$scope.initView = function($uid) {
		$http({
			method: 'post',
			url: apiRoot,
			data: {
				action: 'Group.groupQuery',
				uid: $uid,
			}
		}).then(function successCallback(response) {
			$scope.group=response.data.data;
			console.log(JSON.stringify($scope.group));
			var group_idStr=plus.storage.getItem('repleaseGroup');
			setTimeout(function () {
		      if(group_idStr){
		      	group_idArray=group_idStr.split(",");		      	
		      	for(var i=0;i<group_idArray.length;i++){
		      		$('#groups #'+group_idArray[i]).attr('checked',false);
		      		plus.storage.removeItem('repleaseGroup');
		      	}
		      }
			},50);
		}, function errorfunction(e) {
			console.log(JSON.stringify(e));
		})
	}
    $scope.group_array = function(){
    	var groups_arr=[];
    	$('#groups input[type=checkbox]:checked').each(function(){
    		groups_arr.push($(this).val()+'');
    	})
    	$group_str=groups_arr.join(',');
    	//判断用户是否选组的关联的群
    	if(groups_arr.length == 0){
    		mui.toast('请选择关联的群');return;
    	}
    	var parentid = plus.webview.getWebviewById('release_viewMedia.html');
    	plus.storage.setItem('repleaseGroup',$group_str);
		var parentid = plus.webview.getWebviewById('release_viewMedia.html');
        mui.fire(parentid,'group_num',{group_num:groups_arr.length});		
    	plus.webview.currentWebview().close();
    }
    $scope.choose_allGroup = function(){
    	$('input[type=checkbox]').attr('checked',true);
    }
})


document.addEventListener("plusready",function(){
	appElement=document.querySelector('[ng-controller=addgroupController]');
	$scope= angular.element(appElement).scope();
	plus.nativeUI.closeWaiting();
	ws=plus.webview.currentWebview();
	if(ws.group_id){
      $scope.initView(ws.group_id);
	}
	$scope.$apply();
})
